package com.ocom.payconfig.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.dto.SubsidyDto;
import com.ocom.common.entity.card.EntityXfAccountInfo;
import com.ocom.common.entity.card.EntityXfRecord;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.entity.payconfig.EntitySubsidyData;
import com.ocom.common.entity.payconfig.EntitySubsidyPlan;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.AddSubsidyDataByPersonRequest;
import com.ocom.common.request.payconfig.AddSubsidyDataResquest;
import com.ocom.common.request.payconfig.SubsidyDataRequest;
import com.ocom.common.request.payconfig.SubsidyListRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.response.web.SubsidyListResponse;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtil;
import com.ocom.common.vo.payconfig.SubsidyDataVo;
import com.ocom.payconfig.client.MqSubsidyFeign;
import com.ocom.payconfig.client.XfRecordFeign;
import com.ocom.payconfig.service.*;
import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.*;

@RestController
public class SubsidyController {

    @Autowired
    private SubsidyPlanService subsidyPlanService;

    @Autowired
    private SubsidyDataService subsidyDataService;

    @Autowired
    private XfAccountInfoService xfAccountInfoService;

    @Autowired
    private SubsidyDataVoService subsidyDataVoService;


    @Autowired
    private XfRecordFeign xfRecordFeign;

    @Autowired
    private PersonnelInfoService personnelInfoService;

    @Autowired
    private MqSubsidyFeign mqSubsidyFeign;

    /**
     * @param subsidyListRequest
     * @return com.ocom.common.resp.Result
     * @Description:补贴计划列表（subsidyList）
     * @author lu
     * @date 2022/11/17 14:22
     */
    @PostMapping(PayConfigUrl.Url_subsidyList)
    public Result subsidyList(@Valid @RequestBody SubsidyListRequest subsidyListRequest) {
        List<EntitySubsidyPlan> list = subsidyPlanService.subsidyList(subsidyListRequest.getComId(), subsidyListRequest);

        List<EntitySubsidyPlan> SubsidyPlanList = null;
        Long total = 0L;
        if (subsidyListRequest.getSize() != -1 && subsidyListRequest.getPage() != -1) {
            PageInfo<EntitySubsidyPlan> getListRechargeRVoPageInfo = new PageInfo<>(list);
            SubsidyPlanList = getListRechargeRVoPageInfo.getList();
            total = getListRechargeRVoPageInfo.getTotal();
        } else {
            SubsidyPlanList = list;
        }

        String strPlanID = "";
        for (int i = 0; i < SubsidyPlanList.size(); i++) {
            if (i == 0) {
                strPlanID = String.valueOf(SubsidyPlanList.get(i).getPlanId());
            } else {
                strPlanID = strPlanID + "," + SubsidyPlanList.get(i).getPlanId();
            }
        }
        Map<String, Object> map = new HashMap<>();
        List<SubsidyListResponse> listResponses = new ArrayList<>();
        for (EntitySubsidyPlan entitySubsidyPlan : SubsidyPlanList) {
            SubsidyListResponse subsidyListResponse = new SubsidyListResponse();
            subsidyListResponse.setSubsidyId(entitySubsidyPlan.getPlanId());
            subsidyListResponse.setMonth(entitySubsidyPlan.getMonthValue());
            subsidyListResponse.setTimes(Integer.valueOf(entitySubsidyPlan.getTimes()));
            subsidyListResponse.setWallet(entitySubsidyPlan.getWallet() != null ? entitySubsidyPlan.getWallet() : 0);
            subsidyListResponse.setSubAccount(entitySubsidyPlan.getSubAccount());
            subsidyListResponse.setStatus(Integer.valueOf(entitySubsidyPlan.getStatus()));
            subsidyListResponse.setRemark(entitySubsidyPlan.getRemark());
            if (entitySubsidyPlan.getPlanDate() != null) {
                subsidyListResponse.setPlanDate(DateUtils.getStrDate(entitySubsidyPlan.getPlanDate(), "yyyy-MM-dd"));
            }
            subsidyListResponse.setClearFlag(Integer.valueOf(entitySubsidyPlan.getClearFlag()));
            if (entitySubsidyPlan.getGrantTime() != null) {
                subsidyListResponse.setCompleteTime(DateUtils.getStrDate(entitySubsidyPlan.getGrantTime(), "yyyy-MM-dd HH:mm:ss"));
            }
            listResponses.add(subsidyListResponse);
        }
        if (strPlanID != "") {
            List<EntitySubsidyData> subsidyDataList = subsidyDataService.subsidyList(subsidyListRequest.getComId(), strPlanID);
            for (SubsidyListResponse listRespons : listResponses) {
                listRespons.setPlanNumber(0);
                listRespons.setPlanAmount(0l);
                listRespons.setCompleteNumber(0);
                listRespons.setCompleteAmount(0l);
                listRespons.setClearNumber(0);
                listRespons.setClearAmount(0);
                for (EntitySubsidyData entitySubsidyData : subsidyDataList) {
                    if (listRespons.getTimes().compareTo(Integer.valueOf(entitySubsidyData.getTimes())) == 0 && listRespons.getMonth().equals(entitySubsidyData.getMonthValue())) {
                        listRespons.setPlanNumber(entitySubsidyData.getPlanNumber());
                        listRespons.setPlanAmount(entitySubsidyData.getPlanAmount());
                        listRespons.setCompleteNumber(entitySubsidyData.getCompleteNumber());
                        listRespons.setCompleteAmount(entitySubsidyData.getCompleteAmount());
                        listRespons.setClearNumber(entitySubsidyData.getClearNumber());
                        listRespons.setClearAmount(entitySubsidyData.getClearAmount());
                    }
                }
            }
        }

        map.put("total", total);
        map.put("list", listResponses);
        return ResultUtil.success(map);
    }

    @PostMapping(PayConfigUrl.Url_subsidyComIdList)
    public Result subsidyComIdList(Long comId, String beginMonth) {
        return ResultUtil.success(subsidyPlanService.subsidyListComId(comId, beginMonth));
    }

    @PostMapping(PayConfigUrl.Url_newSubsidyPlan)
    public Result newSubsidyPlan(@RequestBody JSONObject jsonObject) {
        EntitySubsidyPlan newSubsidyPlan = subsidyPlanService.getNewSubsidyPlan(jsonObject.getLong("comId"), jsonObject);
        if (newSubsidyPlan != null) {
            return ResultUtil.error(ResultEnum.Err_4057);
        }
        EntitySubsidyPlan entitySubsidyPlan = new EntitySubsidyPlan();
        entitySubsidyPlan.setComId(jsonObject.getLong("comId"));
        entitySubsidyPlan.setMonthValue(jsonObject.getString("month"));
        entitySubsidyPlan.setTimes(jsonObject.getByte("times"));

        entitySubsidyPlan.setWallet(jsonObject.getIntValue("wallet"));
        entitySubsidyPlan.setSubAccount(jsonObject.getIntValue("subAccount"));
        entitySubsidyPlan.setTimes(jsonObject.getByte("times"));
        entitySubsidyPlan.setRemark(jsonObject.getString("remark"));
        entitySubsidyPlan.setStatus((byte) 1);
        Date date = new Date();
        entitySubsidyPlan.setCreateTime(date);
        entitySubsidyPlan.setUpdateTime(date);
        entitySubsidyPlan.setCreateUserid(jsonObject.getInteger("userId"));
        entitySubsidyPlan.setUpdateUserid(jsonObject.getInteger("userId"));
        if (subsidyPlanService.newSubsidyPlan(entitySubsidyPlan.getComId(), entitySubsidyPlan)) {
            Map<String, Object> map = new HashMap<>();
            map.put("subsidyId", entitySubsidyPlan.getPlanId());
            return ResultUtil.success(map);
        } else {
            return ResultUtil.error(ResultEnum.Err_2016);
        }
    }

    /**
     * @param jsonObject
     * @return com.ocom.common.resp.Result
     * @Description:复制补贴数据（cloneSubsidyData）
     * @author lu
     * @date 2022/11/18 10:17
     */
    @PostMapping(PayConfigUrl.Url_cloneSubsidyData)
    public Result cloneSubsidyData(@RequestBody JSONObject jsonObject) {
        List<EntitySubsidyData> list = subsidyDataService.cloneSubsidyData(jsonObject.getLong("comId"), jsonObject);
        EntitySubsidyPlan newSubsidyPlan = subsidyPlanService.getNewSubsidyPlan(jsonObject.getLong("comId"), jsonObject);
        List<EntitySubsidyData> saveEntitySubsidyData = new ArrayList<>();
        for (EntitySubsidyData entitySubsidyData : list) {
            EntitySubsidyData subsidyData = new EntitySubsidyData();
            subsidyData.setComId(jsonObject.getLong("comId"));
            subsidyData.setPlanId(newSubsidyPlan.getPlanId());
            subsidyData.setMonthValue(jsonObject.getString("month"));
            subsidyData.setTimes(jsonObject.getByte("times"));
            subsidyData.setWallet(jsonObject.getIntValue("wallet"));
            subsidyData.setDataStatus((byte) 1);
            subsidyData.setPId(entitySubsidyData.getPId());
            subsidyData.setPlanMoney(entitySubsidyData.getPlanMoney());
            String orderNo = String.format("%08d", newSubsidyPlan.getPlanId()) + "P" + String.format("%08d", entitySubsidyData.getPId());
            subsidyData.setOrderNo(orderNo);
            Date date = new Date();
            subsidyData.setCreateTime(date);
            subsidyData.setUpdateTime(date);
            if (jsonObject.get("userid") != null) {
                subsidyData.setCreateUserid(jsonObject.getLong("userid"));
                subsidyData.setUpdateUserid(jsonObject.getLong("userid"));
            }
            saveEntitySubsidyData.add(subsidyData);
        }
        if (subsidyDataService.subsidyDataSaves(jsonObject.getLong("comId"), saveEntitySubsidyData)) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ResultEnum.Err_2016);
        }
    }

    @PostMapping(PayConfigUrl.Url_updateSubsidyPlan)
    public Result updateSubsidyPlan(@RequestBody JSONObject jsonObject) {
        EntitySubsidyPlan newSubsidyPlan = subsidyPlanService.getNewSubsidyPlan(jsonObject.getLong("comId"), jsonObject);
        if (newSubsidyPlan == null) {
            return ResultUtil.error(ResultEnum.Err_4058);
        }
        if (newSubsidyPlan.getStatus() == 1 || newSubsidyPlan.getStatus() == 2) {
            if (jsonObject.getInteger("status") == 4) {
                return ResultUtil.error(ResultEnum.Err_4059);
            }
        } else if (newSubsidyPlan.getStatus() == 3) {
            if (jsonObject.getInteger("status") == 1 || jsonObject.getInteger("status") == 2) {
                return ResultUtil.error(ResultEnum.Err_4059);
            }
        } else if (newSubsidyPlan.getStatus() == 4) {
            if (jsonObject.getInteger("status") == 1 || jsonObject.getInteger("status") == 2 || jsonObject.getInteger("status") == 3) {
                return ResultUtil.error(ResultEnum.Err_4059);
            }
        } else if (newSubsidyPlan.getStatus() == 5) {
            if (jsonObject.getInteger("status") == 1 || jsonObject.getInteger("status") == 2 || jsonObject.getInteger("status") == 3 || jsonObject.getInteger("status") == 4) {
                return ResultUtil.error(ResultEnum.Err_4059);
            }
        }
        EntitySubsidyPlan updateSubsidyPlan = new EntitySubsidyPlan();
        updateSubsidyPlan.setPlanId(newSubsidyPlan.getPlanId());
        updateSubsidyPlan.setStatus(jsonObject.getByte("status"));
        updateSubsidyPlan.setRemark(jsonObject.getString("remark"));
        if (jsonObject.getInteger("status") == 2 && jsonObject.get("planDate") == null) {
            return ResultUtil.error(ResultEnum.Err_4060);
        }
        if (StringUtil.isNotEmpty(jsonObject.getString("planDate"))) {
            try {
                updateSubsidyPlan.setPlanDate(DateUtils.getDate(jsonObject.getString("planDate"), "yyyy-MM-dd"));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        updateSubsidyPlan.setClearFlag(jsonObject.getByte("clearFlag"));
        updateSubsidyPlan.setUpdateTime(new Date());
        updateSubsidyPlan.setUpdateUserid(jsonObject.getInteger("userid"));
        if (subsidyPlanService.updateSubsidyPlan(jsonObject.getLong("comId"), updateSubsidyPlan)) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ResultEnum.Err_3026);
        }
    }


    @PostMapping(PayConfigUrl.Url_grant_subsidy)
    public Result grantSubsidy(Long comId, String month, Integer times) {
        //1、查询补贴计划状态
        EntitySubsidyPlan entitySubsidyPlan = subsidyPlanService.selectStatus(comId, month, times);
        if (entitySubsidyPlan == null) {
            return ResultUtil.success();
        }
        //如果status==2，修改状态：
        if (entitySubsidyPlan.getStatus() == 2) {
            subsidyPlanService.updateStatus(comId, entitySubsidyPlan.getPlanId());
        }

        //2、查找待补贴数据subsidyData
        List<EntitySubsidyData> list = subsidyDataService.StaySubsidyData(comId, entitySubsidyPlan.getPlanId());


        if (entitySubsidyPlan.getSubAccount() == 10001) {
            for (EntitySubsidyData entitySubsidyData : list) {
                SubsidyDto subsidyDto = new SubsidyDto();
                    subsidyDto.setComId(entitySubsidyData.getComId());
                    subsidyDto.setPId(entitySubsidyData.getPId());
                    subsidyDto.setPlanId(entitySubsidyData.getPlanId());
                    subsidyDto.setPlanMoney(entitySubsidyData.getPlanMoney());
                    subsidyDto.setClearFlag(entitySubsidyPlan.getClearFlag());
                    subsidyDto.setWallet(entitySubsidyData.getWallet());
                    subsidyDto.setOrderNo(entitySubsidyData.getOrderNo());
                    subsidyDto.setRemark(entitySubsidyPlan.getRemark());
                mqSubsidyFeign.subsidyPlanccbzhshGrant(subsidyDto);
            }
            return ResultUtil.success();
        } else {
            for (EntitySubsidyData subsidyData : list) {
                //subsidyData
                EntityXfAccountInfo entityXfAccountInfo = xfAccountInfoService.getAccount(comId, subsidyData.getPId());
                //如果没有记录：data_status := -1，跳转e)；
                Long clearMoney = 0L;
                Integer completeMoney = subsidyData.getPlanMoney();
                Integer dataStatus = 0;
                Date date = new Date();
                if (entityXfAccountInfo == null) {
                    dataStatus = -1;
                    subsidyDataService.updateSid(comId, subsidyData.getId(), dataStatus, date);
                    continue;
                }
                //b)如果clear_flag==1且s_bal>0，添加补贴清零记录

                if (entitySubsidyPlan.getClearFlag() == 1 && entityXfAccountInfo.getAccountSubsidyMoney() > 0) {
                    clearMoney = entityXfAccountInfo.getAccountSubsidyMoney();
                    EntityXfRecord entityXfRecord = new EntityXfRecord();
                    entityXfRecord.setOpType(15L);
                    entityXfRecord.setOpTypeDiff(11L);
                    entityXfRecord.setConsState(13);
                    entityXfRecord.setPayState(1);
                    entityXfRecord.setComId(comId);
                    entityXfRecord.setPId(Long.valueOf(subsidyData.getPId()));
                    entityXfRecord.setPName(subsidyData.getPName());
                    entityXfRecord.setSubMoney(-clearMoney);
                    entityXfRecord.setRealMoney(-clearMoney);
                    entityXfRecord.setPCode(subsidyData.getPCode());
                    entityXfRecord.setDeptId(subsidyData.getDeptId());
                    entityXfRecord.setDeptName(subsidyData.getDeptName());
                    entityXfRecord.setConsMoney(-clearMoney);
                    entityXfRecord.setSubBalance(0L);
                    entityXfRecord.setAccountNo(entityXfAccountInfo.getAccountNo());
                    entityXfRecord.setOrderNo("QL" + subsidyData.getOrderNo());
                    entityXfRecord.setMonthValue(DateUtils.getStrDate(date, "yyyyMM"));
                    entityXfRecord.setConsTime(date);
                    entityXfRecord.setCreateTime(date);
                    xfRecordFeign.sveXfRecord(entityXfRecord);
                }


                if(entitySubsidyPlan.getClearFlag() == 0 && subsidyData.getPlanMoney()==0){
                    continue;
                }

                if(subsidyData.getPlanMoney()!=0) {

                    //c)添加补贴记录
                    EntityXfRecord entityXfRecord = new EntityXfRecord();
                    entityXfRecord.setOpType(15L);
                    entityXfRecord.setOpTypeDiff(11L);
                    entityXfRecord.setConsState(12);
                    entityXfRecord.setPayState(1);
                    entityXfRecord.setComId(comId);
                    entityXfRecord.setPId(Long.valueOf(subsidyData.getPId()));
                    entityXfRecord.setPName(subsidyData.getPName());
                    entityXfRecord.setSubMoney(Long.valueOf(completeMoney));
                    entityXfRecord.setRealMoney(Long.valueOf(completeMoney));
                    entityXfRecord.setSubBalance(entityXfAccountInfo.getAccountSubsidyMoney() + completeMoney - clearMoney);
                    entityXfRecord.setAccountNo(entityXfAccountInfo.getAccountNo());
                    entityXfRecord.setOrderNo("BT" + subsidyData.getOrderNo());
                    entityXfRecord.setConsTime(date);
                    entityXfRecord.setCreateTime(date);
                    entityXfRecord.setMonthValue(DateUtils.getStrDate(date, "yyyyMM"));

                    entityXfRecord.setPCode(subsidyData.getPCode());
                    entityXfRecord.setDeptId(subsidyData.getDeptId());
                    entityXfRecord.setDeptName(subsidyData.getDeptName());
                    entityXfRecord.setConsMoney(Long.valueOf(completeMoney));
                    xfRecordFeign.sveXfRecord(entityXfRecord);
                }
                //d)修改账户信息、补贴数据
                try {
                    xfAccountInfoService.updatesubsid(comId, subsidyData.getId(), completeMoney, clearMoney, date, entityXfAccountInfo.getId());
                } catch (Exception e) {

                }

            }

        }

        subsidyPlanService.updateSubsidyPlans(comId, entitySubsidyPlan.getPlanId());
        return ResultUtil.success();
    }

    /**
     * @param subsidyDataRequest
     * @return com.ocom.common.resp.Result
     * @Description:补贴数据列表（subsidyData）
     * @author lu
     * @date 2022/12/1 15:08
     */
    @PostMapping(PayConfigUrl.Url_subsidyData)
    public Result subsidyData(@RequestBody SubsidyDataRequest subsidyDataRequest) {
        List<SubsidyDataVo> list = subsidyDataVoService.selectSubsidyData(subsidyDataRequest.getComId(), subsidyDataRequest);
        PageInfo<SubsidyDataVo> getListRechargeRVoPageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        map.put("total", getListRechargeRVoPageInfo.getTotal());
        map.put("list", getListRechargeRVoPageInfo.getList());
        return ResultUtil.success(map);
    }

    /**
     * @param addSubsidyDataResquest
     * @return com.ocom.common.resp.Result
     * @Description:增加补贴人员（addSubsidyData）
     * @author lu
     * @date 2022/12/5 10:07
     */
    @PostMapping(PayConfigUrl.Url_addSubsidyData)
    public Result addSubsidyData(@RequestBody AddSubsidyDataResquest addSubsidyDataResquest) {
        List<EntityPersonnelInfo> person = null;
        if (addSubsidyDataResquest.getAccType() == null || addSubsidyDataResquest.getAccType() == 0) {
            person = personnelInfoService.selectPerson(addSubsidyDataResquest.getComId(), addSubsidyDataResquest);
        } else {
            person = personnelInfoService.selectPersonVo(addSubsidyDataResquest.getComId(), addSubsidyDataResquest);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("comId", addSubsidyDataResquest.getComId());
        jsonObject.put("month", addSubsidyDataResquest.getMonth());
        jsonObject.put("times", addSubsidyDataResquest.getTimes());
        EntitySubsidyPlan newSubsidyPlan = subsidyPlanService.getNewSubsidyPlan(jsonObject.getLong("comId"), jsonObject);
        List<EntitySubsidyData> list = new ArrayList<>();
        for (EntityPersonnelInfo entityPersonnelInfo : person) {
            EntitySubsidyData entitySubsidyData = new EntitySubsidyData();
            entitySubsidyData.setComId(addSubsidyDataResquest.getComId());
            entitySubsidyData.setPlanId(newSubsidyPlan.getPlanId());
            entitySubsidyData.setMonthValue(addSubsidyDataResquest.getMonth());
            int i = addSubsidyDataResquest.getTimes();
            byte y = (byte) i;
            entitySubsidyData.setTimes(y);
            entitySubsidyData.setWallet(newSubsidyPlan.getWallet());
            entitySubsidyData.setSubAccount(newSubsidyPlan.getSubAccount());
            entitySubsidyData.setDataStatus((byte) 1);
            entitySubsidyData.setPId(Math.toIntExact(entityPersonnelInfo.getPId()));
            entitySubsidyData.setPlanMoney(addSubsidyDataResquest.getPlanMoney());
            String orderNo = String.format("%08d", newSubsidyPlan.getPlanId()) + "P" + String.format("%08d", entityPersonnelInfo.getPId());
            entitySubsidyData.setOrderNo(orderNo);
            Date date = new Date();
            entitySubsidyData.setCreateTime(date);
            entitySubsidyData.setUpdateTime(date);
            entitySubsidyData.setCreateUserid(addSubsidyDataResquest.getUserId());
            entitySubsidyData.setUpdateUserid(addSubsidyDataResquest.getUserId());
            list.add(entitySubsidyData);
        }
        try {
            if (subsidyDataService.subsidyDataSaves(addSubsidyDataResquest.getComId(), list)) {
                return ResultUtil.success();
            } else {
                return ResultUtil.error(ResultEnum.Err_2016);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.success();
        }
    }

    @PostMapping(PayConfigUrl.Url_removeSubsidyData)
    public Result removeSubsidyData(@RequestBody AddSubsidyDataResquest addSubsidyDataResquest) {
        EntitySubsidyPlan newSubsidyPlan = subsidyPlanService.getRemoveSubsidyPlan(addSubsidyDataResquest.getComId(), addSubsidyDataResquest);
        if (newSubsidyPlan == null) {
            return ResultUtil.success();
        }
        Boolean b = subsidyDataService.removeSubsidyData(addSubsidyDataResquest.getComId(), addSubsidyDataResquest, newSubsidyPlan.getPlanId());
        if (b) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ResultEnum.Err_3016);
        }
    }

    /**
     * @param addSubsidyDataResquest
     * @return com.ocom.common.resp.Result
     * @Description:更改补贴金额（updateSubsidyMoney）
     * @author lu
     * @date 2022/12/9 16:00
     */
    @PostMapping(PayConfigUrl.Url_updateSubsidyMoney)
    public Result updateSubsidyMoney(@RequestBody AddSubsidyDataResquest addSubsidyDataResquest) {
        EntitySubsidyPlan newSubsidyPlan = subsidyPlanService.getRemoveSubsidyPlan(addSubsidyDataResquest.getComId(), addSubsidyDataResquest);
        if (newSubsidyPlan == null) {
            return ResultUtil.success();
        }
        Boolean b = subsidyDataService.updateSubsidyMoney(addSubsidyDataResquest.getComId(), addSubsidyDataResquest, newSubsidyPlan.getPlanId());
        if (b) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ResultEnum.Err_3014);
        }
    }


    /**
     * @param addSubsidyDataByPersonRequest
     * @return com.ocom.common.resp.Result
     * @Description:增加补贴数据（addSubsidyDataByPerson）
     * @author lu
     * @date 2023/1/3 14:34
     */
    @PostMapping(PayConfigUrl.Url_addSubsidyDataByPerson)
    public Result addSubsidyDataByPerson(@RequestBody AddSubsidyDataByPersonRequest addSubsidyDataByPersonRequest) {
        if (addSubsidyDataByPersonRequest.getPId() == 0) {
            EntityPersonnelInfo entityPersonnelInfo = personnelInfoService.getDataByPerson(addSubsidyDataByPersonRequest.getComId(), addSubsidyDataByPersonRequest.getPCode());
            if (entityPersonnelInfo == null) {
                return ResultUtil.success();
            }
            addSubsidyDataByPersonRequest.setPId(entityPersonnelInfo.getPId());
        }
        String orderNo = String.format("%08d", addSubsidyDataByPersonRequest.getPlanId()) + "P" + String.format("%08d", addSubsidyDataByPersonRequest.getPId());
        EntitySubsidyData selectSubsidyData = subsidyDataService.selectOrderNo(addSubsidyDataByPersonRequest.getComId(), orderNo);
        if (selectSubsidyData == null) {
            EntitySubsidyData entitySubsidyData = new EntitySubsidyData();
            entitySubsidyData.setComId(addSubsidyDataByPersonRequest.getComId());
            entitySubsidyData.setPlanId(addSubsidyDataByPersonRequest.getPlanId());
            entitySubsidyData.setMonthValue(addSubsidyDataByPersonRequest.getMonth());
            int i = addSubsidyDataByPersonRequest.getTimes();
            byte y = (byte) i;
            entitySubsidyData.setTimes(y);
            entitySubsidyData.setWallet(addSubsidyDataByPersonRequest.getWallet());
            entitySubsidyData.setDataStatus((byte) 1);
            entitySubsidyData.setPId(Math.toIntExact(addSubsidyDataByPersonRequest.getPId()));
            entitySubsidyData.setPlanMoney(addSubsidyDataByPersonRequest.getPlanMoney());
            entitySubsidyData.setOrderNo(orderNo);
            Date date = new Date();
            entitySubsidyData.setCreateTime(date);
            entitySubsidyData.setUpdateTime(date);
            subsidyDataService.insetSubsidyData(addSubsidyDataByPersonRequest.getComId(), entitySubsidyData);
        } else {
            if (selectSubsidyData.getDataStatus() == 1 && selectSubsidyData.getPlanMoney().compareTo(addSubsidyDataByPersonRequest.getPlanMoney()) != 0) {
                subsidyDataService.updatePlanMoney(addSubsidyDataByPersonRequest.getComId(), addSubsidyDataByPersonRequest.getPlanMoney(), orderNo);
            }
        }
        return ResultUtil.success();
    }
}
